package com.stone.post.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stone.common.result.BaseResponse;
import com.stone.common.result.ResultUtils;
import com.stone.post.client.UserClient;
import com.stone.post.model.entity.Post;
import com.stone.post.model.entity.User;
import com.stone.post.service.PostService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/post")
public class PostController {

    @Resource
    private PostService postService;

    @Resource
    private UserClient userClient;

    @GetMapping("/hello")
    public String helloWorld(){
        System.out.println("hello world");
        return "hello world";
    }

    @GetMapping("/get/byId")
    public BaseResponse<Post> getPostById(Long postId){
        return ResultUtils.success(postService.getById(postId));
    }

    @GetMapping("/list/page")
    public BaseResponse<List<Post>> listPostPage(int current , int pageSize){
        Page<Post> postPage = postService.page(new Page<>(current ,pageSize));
        return ResultUtils.success(postPage.getRecords());
    }

    @GetMapping("/list/page/byTitle")
    public BaseResponse<List<Post>> listPostPageByTitle(int current , int pageSize , String title){
        Page<Post> postPage = postService.page(new Page<>(current ,pageSize) , new QueryWrapper<Post>().lambda().eq(Post::getTitle , title));
        return ResultUtils.success(postPage.getRecords());
    }

    @PostMapping("/add")
    public BaseResponse<Long> addPost(@RequestBody Post post){
        postService.save(post);
        return ResultUtils.success(post.getId());
    }

    @DeleteMapping("/delete/byId")
    public void deletePostById(Long postId){
        postService.removeById(postId);
    }

    @PutMapping("/update/byId")
    public void updatePostById(@RequestBody Post post){
        postService.updateById(post);
    }
}